home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / uasm.zip / UASM.C < prev    next >
C/C++ Source or Header  |  1986-04-06  |  22KB  |  873 lines

  1. /*
  2.                 UASM - UNIVERSAL CROSS ASSMEBLER                 uasm.c
  3.                 CUSTOM COMPUTER CONSULTANTS
  4.                 5 April 1986
  5.  
  6. */
  7. #include <stdio.h>
  8. #include <uasm.h>
  9.  
  10. #define NHASH  512
  11. #define HP1    7
  12. #define HP2    2
  13.                         /* Flags                */
  14.  
  15. int     sf, lf, hf, nolist ;
  16. int     pass1, pass2 ;
  17. int     lbl ;
  18. int     info ;
  19. int     valid ;
  20.  
  21.                         /* Numbers              */
  22. int     i, j, k ;
  23. int     kk, r ;
  24. int     lineno, lnspp, pgno ;
  25.  
  26. int     cc ;            /* character count      */
  27. int     mcc ;           /* mark character count */
  28. int     ll ;            /* line length          */
  29. int     maxop ;
  30. int     maxpseudo ;
  31. int     ilc, active ;
  32. int     il[NSEG] ;
  33. int     instl ;
  34. int     parval ;
  35. int     num ;
  36. int     radix ;
  37. int     cput ;          /* ib pointer           */
  38. int     errcnt, esp ;
  39. int     syte, sytx ;
  40.  
  41. unsigned        unpar ;
  42. unsigned        ht ;    /* Record Type                          */
  43. unsigned        ha ;    /* Record Start Address                 */
  44. unsigned        he ;    /* Expected Address of next item        */
  45. unsigned        hc ;    /* Checksum Mod 256                     */
  46. unsigned        hsp ;   /* Hex String Pointer                   */
  47.  
  48. long     start, finish, time() ;
  49. #define  NOW   (0L)
  50.                         /* Characters           */
  51.  
  52. unsigned  char    ch ;            /* last character       */
  53. unsigned  char    nch ;           /* next character       */
  54. unsigned  char    sym ;           /* last symbol          */
  55.                         /* Pointers             */
  56.  
  57. char    *iptr ;         /* Input Buffer         */
  58. char    *src ;          /* Source File Name     */
  59. char    *cp ;           /* General Purpose      */
  60.  
  61.                         /* Character Arrays     */
  62.  
  63. char    hex[13] ;       /* Hex File Name        */
  64. char    lst[13] ;       /* List File Name       */
  65.  
  66. char    segtype[NSEG] ;
  67.  
  68. char    id[AL] ;        /* Last Identifier      */
  69. unsigned  char    ib[256] ;       /* Instruction Byte Queue       */
  70. unsigned  char    ssym[256] ;     /* character symbols    */
  71. unsigned  char    hs[MAXB] ;
  72. char    errstk[25] ;
  73. unsigned  char    pv[200] ;
  74. unsigned  char    px[80] ;
  75.  
  76. int    pj[15] ;
  77.  
  78. int    qhash[NHASH] ;     /* Head indexes for hash chains        */
  79. int    nlook, nprobe ;
  80. int    maxprobe ;
  81.  
  82.                         /* Pointer Arrays       */
  83.  
  84. char    *pc[80], *pseudo[15] ;
  85.  
  86. int     (*pp[200])() ;
  87. int     (*ppo[15])() ;
  88.                         /* I/O Buffers          */
  89.  
  90. char    ibuf[MAXLINE] ;
  91. FILE    *dibuf, *libuf, *hxbuf ;
  92.  
  93. struct  symbol  symtab[NSYM] ;
  94. int     psym[NSYM] ;
  95.  
  96. extern  char    push() ;
  97. extern  char    pop() ;
  98. extern  int     cpmf() ;
  99. extern  char    *strcat() ;
  100.  
  101.  
  102. extern  char    *version ;
  103.  
  104. /*
  105.        Control Programs
  106.  
  107.                main()          Open files, assemble, close files, exit
  108.                assemble()      Process input and create tables
  109.                code()          Generate object output
  110.                list()          Generate Listing
  111. */
  112.  
  113. main(narg,argv)
  114. int  narg;
  115. char **argv;
  116.  
  117. {
  118.         start = time(NOW) ;
  119.  
  120.         puts("\033[2J") ;     /* CLEAR_SCREEN */
  121.  
  122.         puts("Universal Cross Assembler -- V1.4  4/5/86\n") ;
  123.         puts("(c) Custom Computer Consultants, 1986\n") ;
  124.         puts(version) ;
  125.  
  126.         sf = lf = hf = nolist = FALSE ;
  127.  
  128. /*              FILE OPERATIONS                 */
  129.  
  130.         if( --narg > 0  ) {
  131.           dibuf = fopen( *(++argv), "r" ) ;
  132.           if( dibuf == NULL ) abort("Source ?\n")   ;
  133.           src = *argv ; sf = TRUE ;
  134.           }
  135.  
  136.         if( --narg > 0 ) {
  137.           j = 0 ;
  138.           while( **argv != '.' && j < 8 ) {
  139.             hex[j] = lst[j] = *(*argv)++ ;
  140.             ++j ;
  141.             }
  142.           hex[j] = lst[j] = EOS ;
  143.           strcat( hex, ".HEX" )  ;
  144.           strcat( lst, ".LST" )  ;
  145.           ++argv ;
  146.           while( ch = *(*argv)++ ) {
  147.             if((ch == 'L') || (ch == 'l')) {
  148.               libuf = fopen(lst,"w") ;
  149.               if( libuf == NULL ) abort("List ?\n") ;
  150.               lf = TRUE ;
  151.               }
  152.             else if((ch == 'O') || (ch == 'o')) {
  153.                    hxbuf = fopen(hex,"w") ;
  154.                    if( hxbuf == NULL ) abort("Hex ?\n")  ;
  155.                    hf = TRUE ;
  156.                    }
  157.                  else if((ch == 'N') || (ch == 'n')) nolist = TRUE ;
  158.  
  159.             }  /* END while(ch = *(*argv)++ )  */
  160.  
  161.           }    /* END if(--narg > 0)           */
  162.  
  163.         syte = pgno = lnspp = 0 ;
  164.         nlook = nprobe = maxprobe = 0 ;
  165.         setmem(ssym,256,NUL) ;         /* NUL is the token value \200  */
  166.         setmem(qhash,2*NHASH,ERROR) ;
  167.  
  168.         cp = "$+-*/(),;%~|&^'@#.<>\"" ;
  169.         while( ch = *cp++ ) ssym[ch]=ch ;
  170.  
  171.         set_type() ;
  172.  
  173.         maxpseudo = build_pseudo() ;
  174.         maxop = buildopc();
  175.         
  176.         pass1 = sf ? TRUE  : FALSE ;
  177.         pass2 = sf ? FALSE : TRUE  ;
  178.  
  179. /*      Main Assembler Loop                     */
  180.  
  181.         while ( pass1 || pass2 ) {
  182.         if ( pass1 ) puts("Pass 1 : ") ;
  183.         if ( pass2 ) puts("Pass 2 : ") ;
  184.         lineno = 1;
  185.  
  186.         set_il() ;
  187.         
  188.         active = code_seg() ;
  189.         hsp = hc = ht = 0 ;
  190.         ha  = he = ilc = il[active] ;
  191.         errcnt = 0 ;
  192.         radix = 10 ;
  193.  
  194.         while(( getline(ibuf)) != EOF ) {
  195.           info = assemble() ;
  196.           code() ;
  197.           list() ;
  198.           lineno++ ;
  199.           ilc +=  instl ;
  200.           }                             /*  end while on getline(ibuf) */
  201.  
  202.         if ( sf ) {
  203.           fclose(dibuf) ;
  204.           dibuf = pass1 ? fopen(src,"r") : dibuf ;
  205.           if( dibuf == NULL ) abort("Source Reopen ?\n") ;
  206.           }
  207.  
  208.         pass1 = FALSE ;
  209.         pass2 = pass2 ? FALSE : TRUE ;
  210.         finish = time(NOW) ;
  211.         printf("\t%5ld\tseconds\n",finish-start) ;
  212.         }                               /* end while on pass1 || pass2 */
  213.  
  214. /*     Sort the Symbol Table                                            */
  215.  
  216.        if ( syte ) {
  217.          qsort(psym,syte,sizeof(int),cpmf) ;
  218.          }
  219.  
  220.  
  221. /*      Print the Symbol Table                                          */
  222.  
  223.         if( !nolist ) {
  224.           cprintf("\fSymbol Table\n\n") ;
  225.           for ( i = 0 ; i < syte ; i++ ) {
  226.             if ( i % 5 == 0 ) cprintf("\n") ;
  227.             sytx = psym[i] ;
  228.             cprintf("%-12s%04x%c   ",symtab[sytx].name,
  229.                                      symtab[sytx].value,
  230.                                      segtype[symtab[sytx].flags & (NSEG-1)] ) ;
  231.           }
  232.         }
  233.  
  234.         cprintf("\n\n") ;
  235.         if( errcnt > 0 ) {
  236.           cprintf("%04d  ERRORS\n",errcnt) ;
  237.           if( nolist || lf ) printf("\n%04d  ERRORS\n",errcnt) ;
  238.           }
  239.         else {
  240.           cprintf("  NO  ERRORS\n") ;
  241.           if( nolist || lf ) puts("\n  NO  ERRORS\n") ;
  242.           }
  243.  
  244.         if ( lf ) {
  245.           putc(CPMEOF,libuf) ;
  246.           fflush(libuf) ;
  247.           fclose(libuf) ;
  248.           }
  249.  
  250.         if ( hf ) {
  251.           hsout() ;
  252.           fprintf(hxbuf,":00000001FF\n") ;
  253.           putc(CPMEOF,hxbuf) ;
  254.           fflush(hxbuf) ;
  255.           fclose(hxbuf) ;
  256.           }
  257.  
  258.         puts("ASSEMBLY COMPLETE\n");
  259.         finish = time(NOW) ;
  260.         printf("\n%5ld\tseconds\n",finish-start) ;
  261.         printf("%5ld\tlines/minute\n",lineno*60/(finish-start)) ;
  262.         printf("%5d\tprobes\n%5d\tlookups\n",nprobe,nlook) ;
  263.         printf("%5.2f\tprobes/lookup\n",(float)nprobe/(float)nlook) ;
  264.         printf("%5d\tmaximum probe\n",maxprobe) ;
  265.         nprobe = 0 ;
  266.         for (nlook = 0 ; nlook < NHASH ; ++nlook ) {
  267.           if( qhash[nlook] != ERROR ) ++nprobe ;
  268.           }
  269.         printf("%5d\thash entries\n",nprobe) ;
  270.         printf("%5d\tpercent utilization\n",(100*nprobe)/nlook) ;
  271.         exit();
  272. }
  273.  
  274. cpmf(a,b)
  275. int    *a, *b ;
  276. {
  277.        return strcmp(symtab[*a].name,symtab[*b].name) ;
  278. }
  279.  
  280. #define INFO    1
  281. #define NOINFO  0
  282.  
  283.  
  284. assemble()
  285. {
  286.  
  287.         setmem(ib,4,0) ;                /*